<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: HelpEdit.php 362 2010-02-03 08:41:11Z steen.rabol@gmail.com $
 *
 */

Prado::using('Application.Common.Data.PortalHelpRecord');
Prado::using('Application.Common.Data.PortalLanguageRecord');

class HelpEdit extends PortalSystemPage
{
	private $_helpRec = null;

	public function getHelpRecord()
	{
		if($this->_helpRec === null)
		{
			$this->_helpRec	= PortalHelpRecord::finder()->findBypagename_AND_culture($this->Request['pagename'],$this->Application->Culture);
		}

		return $this->_helpRec;
	}

	public function dataBind()
	{
		if($this->HelpRecord !== null)
		{
			$this->HelpContent->Text		= $this->HelpRecord->content;
			$this->HelpGrp->GroupingText	= $this->HelpRecord->title;
			$this->HelpTitle->Text			= $this->HelpRecord->title;
		}

		$this->dlHelpLanguage->DataSource = PortalLanguageRecord::finder()->findAllByactive(1);
		$this->dlHelpLanguage->dataBind();
		$this->dlHelpLanguage->SelectedValue = $this->Application->Culture;
	}

	public function onInit($param)
	{
		parent::onInit($param);

		if(!$this->User)
		{
			$this->reportError(1,Prado::localize("You need to be logged in to use this page"));
		}

		if($this->User->RoleId != $this->Application->Parameters['AdminRole'])
		{
			$this->reportError(1,Prado::localize("You do not have access to this function/page"));
		}
	}

	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Page->Title = Prado::localize("Edit help") . ' - ' . $this->Request['pagename'];

		if(!$this->IsPostBack)
		{
			$this->dataBind();
		}
	}

	public function CancelContent($sender,$param)
	{
		$this->gotoDefaultPage();
	}

	public function SaveContent($sender,$param)
	{
		$helpRecord = PortalHelpRecord::finder()->findBypagename_AND_culture($this->Request['pagename'],$this->dlHelpLanguage->SelectedValue);
		if($helpRecord)
		{
			$helpRecord->content	= $this->HelpContent->Text;
			$helpRecord->title		= $this->HelpTitle->Text;
			$helpRecord->culture	= $this->dlHelpLanguage->SelectedValue;
			$helpRecord->pagename	= $this->Request['pagename'];
		}
		else
		{
			$helpRecord				= new PortalHelpRecord();
			$helpRecord->content	= $this->HelpContent->Text;
			$helpRecord->title		= $this->HelpTitle->Text;
			$helpRecord->pagename	= $this->Request['pagename'];
			$helpRecord->culture	= $this->dlHelpLanguage->SelectedValue;
		}

		$helpRecord->save();
		$this->gotoPage('System.Help',array('pagename' => $helpRecord->pagename));
	}

	public function selectionChanged($sender,$param)
	{
		$this->_helpRec	= PortalHelpRecord::finder()->findBypagename_AND_culture($this->Request['pagename'],$this->dlHelpLanguage->SelectedValue);
		if($this->_helpRec === null)
		{
			$hp = new PortalHelpRecord();
			$hp->pagename	= $this->Request['pagename'];
			$hp->culture	= $this->dlHelpLanguage->SelectedValue;
			$hp->title		= $this->Request['pagename'];
			$hp->content	= 'Auto genereated';
			$hp->save();
			$this->_helpRec	= PortalHelpRecord::finder()->findBypagename_AND_culture($this->Request['pagename'],$this->dlHelpLanguage->SelectedValue);
		}

		$this->HelpContent->Text		= $this->HelpRecord->content;
		$this->HelpGrp->GroupingText	= $this->HelpRecord->title;
		$this->HelpTitle->Text			= $this->HelpRecord->title;
	}
}
?>